package demo1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //奇偶位数
    public int[] evenOddBit(int n) {
        int[] ret = new int[2];
        int even = 0;
        int odd = 0;
        List<Integer> list = new ArrayList<>();
        while (n>0) {
            if (n % 2 == 1) {
                list.add(1);
            }else {
                list.add(0);
            }
            n /= 2;
        }
        int len = list.size();
        for (int i = 0; i < len; i++) {
            if(list.get(i)==1){
                if(i%2==1){
                    odd++;
                }else {
                    even++;
                }
            }
        }
        ret[0] = even;
        ret[1] = odd;
        return ret;
    }

    //彩灯装饰记录 1
    public static int[] levelOrder(TreeNode root) {
        //层序遍历：利用队列实现
        if(root==null) return new int[0];
        Queue<TreeNode> que=new LinkedList<>();
        ArrayList<Integer> ans=new ArrayList<>();
        que.add(root);
        while(!que.isEmpty()){
            TreeNode node=que.poll();
            ans.add(node.val);
            if(node.left!=null) que.add(node.left);
            if(node.right!=null) que.add(node.right);
        }
        int []res=new int[ans.size()];
        for(int i=0;i<ans.size();++i){
            res[i]=ans.get(i);
        }
        return res;
    }
}
